﻿using FangChan.Common;
using McsWeb.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace McsWeb.Controllers
{
    public class StaffsController : BaseController
    {
        #region 列表
        public ActionResult Index()
        {
            var cookies = Web.Common.CookiesManagement.GetCookies();
            var ticket = Web.Common.CookiesManagement.GetTicket(cookies);
            var model = Web.Common.CookiesManagement.GetLoginModel(ticket);

            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query("select * from Staffs where id=" + model.UserID).Tables[0];

            var roleID = Convert.ToInt32(dt.Rows[0]["RoleID"]);

            ViewBag.RoleId = roleID;

            return View();
        }

        public string IndexAsync(string Name, int pageIndex, int pageSize, int RoleID)
        {
            var cookies = Web.Common.CookiesManagement.GetCookies();
            var ticket = Web.Common.CookiesManagement.GetTicket(cookies);
            var model = Web.Common.CookiesManagement.GetLoginModel(ticket);
             
            DataTable Userdt = FangChan.DBUtility.DbHelperSQL.Query("select * from Staffs where id=" + model.UserID).Tables[0];

            var roleID = Convert.ToInt32(Userdt.Rows[0]["RoleID"]);

            if (roleID != 1)
            {
                return null;
            }

            List<StaffsModel> staff = new List<StaffsModel>();

            if (Name == null)
            {
                Name = "";
            }

            int total = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.GetSingle("select dbo.Fn_Value_StaffsNumber('" + Name + "'," + RoleID + ")"));
            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query("select * from dbo.Fn_Table_StaffsList('" + Name + "'," + pageIndex + "," + pageSize + "," + RoleID + ") where ID !=1").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                StaffsModel m = new StaffsModel();
                m.ID = Convert.ToInt32(item["ID"]);
                m.Name = Convert.ToString(item["Name"]);
                m.LoginName = Convert.ToString(item["LoginName"]);
                m.LoginPass = Convert.ToString(item["LoginPass"]);
                m.RoleID = Convert.ToInt32(item["RoleID"]);
                m.QQ1 = Convert.ToString(item["QQ1"]);
                m.QQ2 = Convert.ToString(item["QQ2"]);
                m.QQ3 = Convert.ToString(item["QQ3"]);
                m.WeiXin1 = Convert.ToString(item["WeiXin1"]);
                m.WeiXin2 = Convert.ToString(item["WeiXin2"]);
                m.WeiXin3 = Convert.ToString(item["WeiXin3"]);
                staff.Add(m);
            }
            DataTable fdt = FangChan.DBUtility.DbHelperSQL.Query("select * from dbo.Fn_Table_UsersListFooter(" + model.UserID + ",'" + Name + "')").Tables[0];

            GridData<StaffsModel> gridData = total > 0 ? new GridData<StaffsModel>() { total = total, rows = staff } : new GridData<StaffsModel>() { total = total, rows = new List<StaffsModel>() };

            return gridData.ToJson();
        }
        #endregion

        #region 添加 
        public ActionResult Insert()
        {
            return View();
        }

        public ActionResult InsertAsync(StaffsModel Staffs)
        {
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();

            #region null复制为""
            if (Staffs.Name == null)
            {
                Staffs.Name = "";
            }
            if (Staffs.LoginName == null)
            {
                Staffs.LoginName = "";
            }
            if (Staffs.LoginPass == null)
            {
                Staffs.LoginPass = "";
            }
            if (Staffs.QQ1 == null)
            {
                Staffs.QQ1 = "";
            }
            if (Staffs.QQ2 == null)
            {
                Staffs.QQ2 = "";
            }
            if (Staffs.QQ3 == null)
            {
                Staffs.QQ3 = "";
            }
            if (Staffs.WeiXin1 == null)
            {
                Staffs.WeiXin1 = "";
            }
            if (Staffs.WeiXin2 == null)
            {
                Staffs.WeiXin2 = "";
            }
            if (Staffs.WeiXin3 == null)
            {
                Staffs.WeiXin3 = "";
            }
            #endregion

            string sql = "insert into Staffs values ('" + Staffs.Name + "','" + Staffs.LoginName + "','" + Staffs.LoginPass + "','" + Staffs.RoleID + "','" + Staffs.QQ1 + "','" + Staffs.QQ2 + "','" + Staffs.QQ3 + "','" + Staffs.WeiXin1 + "','" + Staffs.WeiXin2 + "','" + Staffs.WeiXin3 + "')";

            int count = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.ExecuteSql(sql));

            if (count > 0)
            {
                result.Type = AjaxResultType1.Success;
                result.Content = "录入成功";
            }
            else
            {
                result.Type = AjaxResultType1.Error;
                result.Content = "录入失败";
            }
            return JsonEx(result);
        }
        #endregion

        #region 修改
        [Description("结点-列表修改")]
        public ActionResult Edit(int id)
        {
            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query("select * from Staffs where id=" + id).Tables[0];

            StaffsModel Staffs = new StaffsModel();

            Staffs.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
            Staffs.Name = Convert.ToString(dt.Rows[0]["Name"]);
            Staffs.LoginName = Convert.ToString(dt.Rows[0]["LoginName"]);
            Staffs.LoginPass = Convert.ToString(dt.Rows[0]["LoginPass"]);
            Staffs.RoleID = Convert.ToInt32(dt.Rows[0]["RoleID"]);
            Staffs.QQ1 = Convert.ToString(dt.Rows[0]["QQ1"]);
            Staffs.QQ2 = Convert.ToString(dt.Rows[0]["QQ2"]);
            Staffs.QQ2 = Convert.ToString(dt.Rows[0]["QQ3"]);
            Staffs.WeiXin1 = Convert.ToString(dt.Rows[0]["WeiXin1"]);
            Staffs.WeiXin2 = Convert.ToString(dt.Rows[0]["WeiXin2"]);
            Staffs.WeiXin3 = Convert.ToString(dt.Rows[0]["WeiXin3"]);

            ViewBag.entity = Staffs;
            return View();
        }

        [Description("结点-列表修改")]
        public ActionResult EditAsync(StaffsModel Staffs)
        {
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();

            #region null复制为""
            if (Staffs.Name == null)
            {
                Staffs.Name = "";
            }
            if (Staffs.LoginName == null)
            {
                Staffs.LoginName = "";
            }
            if (Staffs.LoginPass == null)
            {
                Staffs.LoginPass = "";
            }
            if (Staffs.QQ1 == null)
            {
                Staffs.QQ1 = "";
            }
            if (Staffs.QQ2 == null)
            {
                Staffs.QQ2 = "";
            }
            if (Staffs.QQ3 == null)
            {
                Staffs.QQ3 = "";
            }
            if (Staffs.WeiXin1 == null)
            {
                Staffs.WeiXin1 = "";
            }
            if (Staffs.WeiXin2 == null)
            {
                Staffs.WeiXin2 = "";
            }
            if (Staffs.WeiXin3 == null)
            {
                Staffs.WeiXin3 = "";
            }
            #endregion

            string sql = "update Staffs set Name='" + Staffs.Name + "',LoginName='" + Staffs.LoginName + "',LoginPass='" + Staffs.LoginPass + "',RoleID='" + Staffs.RoleID + "',QQ1='" + Staffs.QQ1 + "',QQ2='" + Staffs.QQ2 + "',QQ3='" + Staffs.QQ3 + "',WeiXin1='" + Staffs.WeiXin1 + "',WeiXin2='" + Staffs.WeiXin2 + "',WeiXin3='" + Staffs.WeiXin3 + "' where ID=" + Staffs.ID;

            int count = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.ExecuteSql(sql));

            if (count > 0)
            {
                result.Type = AjaxResultType1.Success;
                result.Content = "修改成功";
            }
            else
            {
                result.Type = AjaxResultType1.Error;
                result.Content = "修改失败";
            }
            return JsonEx(result);
        }
        #endregion

        #region 删除
        [Description("结点-列表删除")]
        [HttpPost]
        public ActionResult DeleteAsync(string ids)
        {
            var result = new AjaxResult();
             
            var cookies = Web.Common.CookiesManagement.GetCookies();
            var ticket = Web.Common.CookiesManagement.GetTicket(cookies);
            var model = Web.Common.CookiesManagement.GetLoginModel(ticket);

            try
            {
                string[] idsStr = ids.Split(new char[] { ',' });
                int[] idsInt = Array.ConvertAll(idsStr, id => Convert.ToInt32(id));
                
                for (int i = 0; i < idsInt.Length; i++)
                {

                    string sql = "delete Staffs where id=" + idsInt[i];

                    int count = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.ExecuteSql(sql));
                }

                result.Type = AjaxResultType1.Success;
                result.Content = "删除成功";
            }
            catch
            {
                result.Type = AjaxResultType1.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }

        #endregion
    }
}